<template>
  <div class="login-wrapper">
    <!-- 背景装饰元素 -->
    <div class="bg-pattern"></div>
    <div class="bg-overlay"></div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <!-- 品牌Logo区域 -->
      <div class="logo-container">
        <div class="logo-icon">
          <svg width="60" height="60" viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg">
            <circle cx="50" cy="50" r="45" fill="#409EFF" />
            <path d="M35 60 L65 60 M45 40 L55 40 M35 50 L65 50" stroke="white" stroke-width="5" stroke-linecap="round" />
          </svg>
        </div>
        <h1 class="logo-title">尚融宝管理平台</h1>
        <p class="logo-subtitle">Integral Financial Management</p>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
      >
        <!-- 用户名输入 -->
        <el-form-item prop="username">
          <div class="input-group">
            <span class="input-icon">
              <i class="el-icon-user"></i>
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="用户名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
              @focus="handleFocus($event)"
              @blur="handleBlur($event)"
            />
          </div>
        </el-form-item>

        <!-- 密码输入 -->
        <el-form-item prop="password">
          <div class="input-group">
            <span class="input-icon">
              <i class="el-icon-lock"></i>
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
              @focus="handleFocus($event)"
              @blur="handleBlur($event)"
            />
            <span class="show-pwd" @click="showPwd">
              <i :class="passwordType === 'password' ? 'el-icon-view' : 'el-icon-eye-close'"></i>
            </span>
          </div>
        </el-form-item>

        <!-- 记住密码和忘记密码 -->
        <div class="form-options">
          <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>

        <!-- 登录按钮 -->
        <el-button
          :loading="loading"
          type="primary"
          class="login-button"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>

        <!-- 提示信息 -->
        <div class="tips">
          <span>测试账号: admin</span>
          <span>密码: 111111</span>
        </div>
      </el-form>
    </div>

    <!-- 页脚 -->
    <div class="login-footer">
      <p>© 2025 尚融宝管理平台 版权所有</p>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入有效的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      rememberMe: false,
      focusedField: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? 'text' : 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 模拟登录请求
          setTimeout(() => {
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }, 1000)
        } else {
          this.$message.error('请完成表单验证')
          return false
        }
      })
    },
    handleFocus(event) {
      this.focusedField = event.target.name
    },
    handleBlur() {
      this.focusedField = null
    }
  }
}
</script>

<style lang="scss" scoped>
// 颜色变量
$primary-color: #409EFF;
$secondary-color: #5C7080;
$background-color: #283443;
$card-bg: rgba(255, 255, 255, 0.05);
$input-bg: rgba(255, 255, 255, 0.1);
$input-border: rgba(255, 255, 255, 0.2);
$text-color: #fff;
$text-secondary: #a0a0a0;
$focus-color: #409EFF;

.login-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  background-color: $background-color;
  position: relative;
  overflow: hidden;

  // 背景装饰
  .bg-pattern {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image:
      radial-gradient(rgba(255, 255, 255, 0.1) 2px, transparent 2px),
      radial-gradient(rgba(255, 255, 255, 0.1) 2px, transparent 2px);
    background-size: 50px 50px;
    background-position: 0 0, 25px 25px;
    opacity: 0.3;
    animation: bgMove 20s linear infinite;
  }

  .bg-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, rgba(64, 158, 255, 0.1) 0%, rgba(0, 0, 0, 0.3) 100%);
  }

  // 登录卡片
  .login-card {
    position: relative;
    width: 400px;
    max-width: 90%;
    background: $card-bg;
    border-radius: 16px;
    backdrop-filter: blur(10px);
    border: 1px solid $input-border;
    padding: 40px 30px;
    z-index: 10;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    animation: fadeInUp 0.6s ease-out;

    // 品牌Logo区域
    .logo-container {
      text-align: center;
      margin-bottom: 40px;

      .logo-icon {
        display: inline-block;
        margin-bottom: 15px;
        transform: translateY(0);
        transition: transform 0.3s ease;

        &:hover {
          transform: translateY(-5px);
        }
      }

      .logo-title {
        font-size: 24px;
        font-weight: 600;
        color: $text-color;
        margin-bottom: 8px;
        letter-spacing: 1px;
      }

      .logo-subtitle {
        font-size: 14px;
        color: $text-secondary;
        opacity: 0.8;
      }
    }

    // 表单样式
    .login-form {
      .input-group {
        position: relative;
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        .input-icon {
          position: absolute;
          left: 15px;
          color: $text-secondary;
          font-size: 16px;
          transition: color 0.3s;
        }

        .el-input {
          width: 100%;

          .el-input__inner {
            height: 48px;
            line-height: 48px;
            background: $input-bg;
            border: 1px solid $input-border;
            border-radius: 8px;
            color: $text-color;
            padding-left: 45px;
            padding-right: 40px;
            transition: all 0.3s;

            &:focus {
              border-color: $focus-color;
              box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
            }
          }
        }

        // 显示密码按钮
        .show-pwd {
          position: absolute;
          right: 15px;
          color: $text-secondary;
          cursor: pointer;
          transition: color 0.3s;

          &:hover {
            color: $text-color;
          }
        }
      }

      // 表单选项（记住密码和忘记密码）
      .form-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 25px;

        .el-checkbox {
          .el-checkbox__label {
            color: $text-secondary;
          }

          .el-checkbox__input.is-checked .el-checkbox__inner {
            background-color: $primary-color;
            border-color: $primary-color;
          }
        }

        .forgot-password {
          color: $text-secondary;
          text-decoration: none;
          transition: color 0.3s;

          &:hover {
            color: $primary-color;
          }
        }
      }

      // 登录按钮
      .login-button {
        height: 48px;
        border-radius: 8px;
        font-size: 16px;
        font-weight: 500;
        transition: all 0.3s;

        &:hover:not(.is-disabled) {
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
        }

        &:active:not(.is-disabled) {
          transform: translateY(0);
        }
      }

      // 提示信息
      .tips {
        font-size: 13px;
        color: $text-secondary;
        margin-top: 20px;
        text-align: center;

        span {
          margin: 0 10px;
        }
      }
    }
  }

  // 页脚
  .login-footer {
    position: absolute;
    bottom: 20px;
    color: $text-secondary;
    font-size: 13px;
    opacity: 0.7;
  }
}

// 动画效果
@keyframes bgMove {
  0% {
    background-position: 0 0, 25px 25px;
  }
  100% {
    background-position: 50px 50px, 75px 75px;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
